from django.shortcuts import render
from rest_framework.generics import ListAPIView
from .models import SKU
from .serializers import HotSkuListSerializer
from rest_framework.filters import OrderingFilter
"""
热销商品获取
1,前端携带分类编号
2,后端根据编号查询2条热销商品
3,组织数据返回(id,name,price,default_image)
"""
class HotSkuListView(ListAPIView):
    pagination_class = None  # 关闭分页
    serializer_class = HotSkuListSerializer
    # queryset = SKU.objects.filter(catetory_id=category_id).order_by("-sales")[0:2]

    def get_queryset(self):

        #self.kwargs获取的是地址中,或者?后面获取的参数
        category_id = self.kwargs["category_id"]

        return SKU.objects.filter(category_id=category_id).order_by("-sales")[0:2]


"""
商品列表
1, 前端传递排序的参数(默认,价格,人气)
2, 根据对应的排序方式,查询数据返回
"""
class SkuListView(ListAPIView):
    serializer_class = HotSkuListSerializer
    # queryset = SKU.objects.all()

    def get_queryset(self):
        category_id = self.kwargs["category_id"]
        return SKU.objects.filter(category_id=category_id)

    #1,指定排序方式
    filter_backends = [OrderingFilter]
    ordering_fields = ("create_time","price","sales") #前端调用方式ordering=指定的排序字段

    #2,指定分页
    # pagination_class = LimitOffsetPagination #limit=限定几条
    # pagination_class = PageNumberPagination #page=4
    # pagination_class = CustomPageNumberPagination



from .serializers import SKUIndexSerializer
from drf_haystack.viewsets import HaystackViewSet

class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer

